clear
version 14.0
set more off
set scheme s2mono

* Specify the year of cash rent data to use in the regression
local reg_year 2013
* Specify 1 if irrigated or 0 if nonirrigated
local practice 0

capture log close
if `practice'==0 {
log using "..\logs\main results `reg_year' - heterogeneity.txt", text replace
}
if `practice'==1 {
log using "..\logs\main results irrigated `reg_year' - heterogeneity.txt", text replace
}

* run the do-files to define programs used in this code
run "..\codeSetup\soils transform - program.do"
run "main results - program heterogeneity.do"

local bootstrap_reps 1000

*------------------------------------------------------------------------
* Potential soils
*------------------------------------------------------------------------
local soils  slope om sar gypsum cec7 ec  ksat clay_perc silt_perc ///
			bulkDensity soc0_150 rootznemc ph_less6 ph_greater7dot5 hydgrp* text_*

local soils_nonlin  slope_* om_* sar_* gypsum_* cec7_* ec_*  ksat_* clay_perc_* silt_perc_* ///
			bulkDensity_* soc0_150_* rootznemc_* ph_less6 ph_greater7dot5 hydgrp* text_*

*------------------------------------------------------------------------
* Controls to include in the regression
*------------------------------------------------------------------------
local deficit water_deficit
local surplus water_surplus
local edd dday34C
local soils_controls soc0_150_4 bulkDensity_4 ec_2 ph_less6 ph_greater7dot5 slope_1     
*local soils_controls $SelSoils

local knots_`deficit' 2
local knots_`surplus' 2
local knots_gdd 3
local knots_`edd' 3

local main_year 2013

*------------------------------------------------------------------------
* Regression
*------------------------------------------------------------------------

use "..\dataAnalysis\rent_climate_data", clear

if `practice'==0{
gen ln_cash_rent=ln(cash_rent_non`reg_year')
}
if `practice'==1{
gen ln_cash_rent=ln(cash_rent_irr`reg_year')
}

capture drop `deficit'_spline*
gen `deficit'_spline = `deficit'
matrix `deficit'_knots=[0]

capture drop water_surplus_spline*
gen `surplus'_spline = `surplus'
matrix `surplus'_knots=[0]

capture drop gdd_spline*
mkspline gdd_spline = gdd, cubic nknots(`knots_gdd') 
matrix gdd_knots=r(knots)

capture drop `edd'_spline*
mkspline `edd'_spline = `edd', cubic nknots(`knots_`edd'') 
matrix `edd'_knots=r(knots)		

gen my_sandy=text_sandy + text_mod_sandy
gen my_clay=text_mod_clay + text_clay
gen my_silt=text_medium
summ my_sandy my_clay my_silt

reg ln_cash_rent `deficit'_spline* c.`deficit'_spline*#c.my_clay c.`deficit'_spline*#c.my_sandy ///
				`surplus'_spline* c.`surplus'_spline*#c.my_clay c.`surplus'_spline*#c.my_sandy  ///
				gdd_spline* c.gdd_spline*#c.my_clay c.gdd_spline*#c.my_sandy  ///
				`edd'_spline* c.`edd'_spline*#c.my_clay c.`edd'_spline*#c.my_sandy  ///
				c.my_clay c.my_sandy `soils_controls' i.stfips ///
			[aw=croplandNon_acres], vce(cluster stfips)


		
ricardian_program_het, deficit("`deficit'") surplus("`surplus'") edd("`edd'") ///
			knots_d(`knots_`deficit'') knots_s(`knots_`surplus'') knots_g(`knots_gdd') knots_e(`knots_`edd'') ///
			practice(`practice') reg_year(`reg_year') soils_controls(`soils_controls') scenario("rcp45") model("AVG") wild(0) 

ereturn display
matrix beta_main=e(b)

est restore main_results

			
simulate _b, seed(2030) reps(`bootstrap_reps') : ricardian_program_het, deficit("`deficit'") surplus("`surplus'") edd("`edd'") ///
			knots_d(`knots_`deficit'') knots_s(`knots_`surplus'') knots_g(`knots_gdd') knots_e(`knots_`edd'') ///
			reg_year(`reg_year') soils_controls(`soils_controls') scenario("rcp45") model("AVG") wild(1)

qui corr _all, cov
matrix var=r(C)

matrix colnames var = rel_total_chg rel_deficit_chg rel_surplus_chg rel_heat_chg ///
				share_deficit share_surplus share_heat 
matrix rownames var = rel_total_chg rel_deficit_chg rel_surplus_chg rel_heat_chg ///
				share_deficit share_surplus share_heat 

ereturn post beta_main var, dof(14)
ereturn display		

log close
